package movie.py;

import java.io.*;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class MovieMr {

    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void init(String filePath){
        try {
            File csv = new File(filePath); // CSV数据文件

            BufferedWriter bw = new BufferedWriter(new FileWriter(csv, true)); // 附加


            File data = new File("E:\\code\\ideawork\\hadoopone\\src\\main\\java\\movie\\file\\u.data"); // CSV数据文件

            BufferedReader br = new BufferedReader(new FileReader(data));
            while (br.ready()) {
                String line = br.readLine();
                //去除空格
                line = line.trim();
                //挨个赋值
                String[] clos = line.split("\t");
                String userid = clos[0];
                String movieid = clos[1];
                String rating = clos[2];
                String unixtime = clos[3];

                String sd = sdf.format(new Date(Long.parseLong(unixtime)* 1000L)); // 时间戳转换日期
                LocalDateTime time = LocalDateTime.from(formatter.parse(sd));
                //LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(unixtime)), ZoneId.systemDefault());

                int weekday = time.getDayOfWeek().getValue();
                // 添加新的数据行
                bw.write( String.format("%s,%s,%s,%s", userid, movieid, rating, weekday));
                bw.newLine();
// 数据元素分为一行
                // StringTokenizer st = new StringTokenizer(line, ",");
                //  while (st.hasMoreTokens()) {
                // 每个元素都显示在一个制表符分隔的线
                //      System.out.print(st.nextToken() + "\t");
                // }
            }
            br.close();
            bw.close();

        } catch (Exception e) {
            // File对象的创建过程中的异常捕获
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        init("F:/writers.csv");
    }
}
